<template>
    <view>
        <!-- pages/order/msg.wxml -->
        <!-- <header name="订单详情" type="1"></header> -->
        <view class="orderstatus">
            <image src="/static/ordermsgbg.png" mode="aspectFill" class="orderbg"></image>
            <!-- 待支付 -->
            <view v-if="orderinfo.status == 0">
                <image src="/static/waitpay.png" mode="aspectFit"></image>
                <text>等待付款</text>
            </view>
            <text class="price" v-if="orderinfo.status == 0">
                应付金额：￥
                <text>{{ orderinfo.payprice }}</text>
            </text>
            <text class="gopay" @tap="gopay" :data-id="orderinfo.orderid" v-if="orderinfo.status == 0">去支付</text>
            <!-- 待收货 -->
            <view v-if="orderinfo.status == 2">
                <image src="/static/waitshouhuo.png" mode="aspectFit"></image>
                <text>等待收货</text>
            </view>
            <text class="price" v-if="orderinfo.status >= 2">{{ orderinfo.fuwu }}</text>
            <text class="gopay" v-if="orderinfo.status == 1" @tap.stop.prevent="cuidan">我要催单</text>
            <!-- 待发货 -->
            <view v-if="orderinfo.status == 1">
                <image src="/static/waitshouhuo.png" mode="aspectFit"></image>
                <text>等待发货</text>
            </view>
            <text class="price" v-if="orderinfo.status == 1">
                已付金额：￥
                <text>{{ orderinfo.payprice }}</text>
            </text>
            <text class="gopay" v-if="orderinfo.status == 1" @tap.stop.prevent="cuidan">我要催单</text>
            <!-- 已完成 -->
            <view v-if="orderinfo.status == 4">
                <image src="/static/wancheng.png" mode="aspectFit"></image>
                <text>交易完成</text>
            </view>
            <text class="gopay" v-if="orderinfo.status == 4">再次购买</text>
        </view>
        <view class="orderaddr">
            <view class="orderstatusmsg" v-if="item.wuliu.status">
                <view>
                    <image src="/static/kuaidi.png" mode="aspectFit"></image>
                    <text>
                        {{ item.wuliu.list[0]['status'] }}
                        <text>{{ item.wuliu.korderid }}</text>
                    </text>
                </view>
                <text>{{ item.wuliu.list[0]['time'] }}</text>
                <image src="/static/arrowright.png" mode="aspectFit" class="arrowright"></image>
            </view>
            <view class="user">
                <image src="/static/phoneuser.png" mode="aspectFit"></image>
                <text>{{ orderinfo.address.name }} {{ orderinfo.address.phone }}</text>
            </view>
            <view class="address">
                <image src="/static/loc.png" mode="aspectFit"></image>
                <text>{{ orderinfo.address.address }}</text>
            </view>
        </view>
        <view class="goodsmsg" v-for="(item, index) in goodslist" :key="index">
            <image :src="item.goods.pics[0]" mode="aspectFill"></image>

            <view>
                <text class="title">{{ item.goods.title }}</text>
                <text class="tips">{{ item.formatguige }}</text>
                <view class="price">
                    <text>￥{{ item.goodsprice }}</text>
                    <text>x{{ item.num }}</text>
                </view>
            </view>
        </view>
        <view class="ordermsg">
            <view class="orderlist">
                <text>订单编号：</text>
                <view>
                    <text>{{ orderinfo.orderid }}</text>
                    <text class="copy" @tap="copy">复制</text>
                </view>
            </view>
            <view class="orderlist">
                <text>下单时间：</text>
                <view>
                    <text>{{ orderinfo.time0[1] }}</text>
                </view>
            </view>
            <view class="orderlist" v-if="orderinfo.status != 0">
                <text>支付方式：</text>
                <view>
                    <text>{{ paytype }}</text>
                </view>
            </view>
            <view class="orderlist">
                <text>配送服务：</text>
                <view>
                    <text>
                        {{
                            orderinfo.ordertype == 1
                                ? '快递配送'
                                : orderinfo.ordertype == 2
                                ? '到店自提'
                                : orderinfo.ordertype == 3
                                ? '到店核销'
                                : orderinfo.ordertype == 4
                                ? '虚拟商品'
                                : '线上商品'
                        }}
                    </text>
                </view>
            </view>
            <view class="orderlist1">
                <text>商品总额</text>
                <text>￥{{ orderinfo.sumprice }}</text>
            </view>
            <view class="orderlist1">
                <text>运费</text>
                <text>￥{{ orderinfo.yunprice }}</text>
            </view>
            <view class="total">
                <text>
                    总计: ￥
                    <text>{{ orderinfo.payprice }}</text>
                </text>
            </view>
        </view>

        <view class="orderbtn">
            <text v-if="orderinfo.status == 0" @tap.stop.prevent="delorder" :data-id="orderinfo.orderid">删除订单</text>
            <text v-if="orderinfo.status == 4" @tap.stop.prevent="delorder" :data-id="orderinfo.orderid">删除订单</text>
            <text v-if="orderinfo.status == 0" class="active" @tap="gopay" :data-id="orderinfo.orderid">去支付</text>
            <text class="active" v-if="orderinfo.status == 1" @tap.stop.prevent="cuidan">我要催单</text>
            <text v-if="orderinfo.status == 4" @tap.stop.prevent="buyagain">再次购买</text>
            <text class="active" v-if="orderinfo.status == 2" @tap.stop.prevent="shouhuo" :data-id="orderinfo.orderid">确认收货</text>
            <!-- <text wx:if="{{orderinfo.btns[5]}}">评价晒单</text> -->
            <!-- <text wx:if="{{orderinfo.btns[6]}}">删除订单</text> -->

            <!-- <text wx:if="{{orderinfo.btn[0]}}">退换/售后</text> -->
            <!-- <text wx:if="{{orderinfo.btn[0]}}">追加评价 </text> -->
            <!-- <text wx:if="{{orderinfo.btn[0]}}">我要催单</text> -->
            <!-- <text wx:if="{{orderinfo.btn[0]}}">申请退款</text> -->
        </view>
    </view>
</template>

<script>
import header from '../../components/header/headerBar.vue';
import list from '../../components/list/index';
// pages/order/msg.js
const app = getApp();
export default {
    components: {
        header,
        list
    },
    data() {
        return {
            paytype: '',
            nums: [],
            gids: [],
            guiges: [],
            orderid: '',

            orderinfo: {
                status: 0,
                payprice: '',
                orderid: '',
                fuwu: '',

                address: {
                    name: '',
                    phone: '',
                    address: ''
                },

                time0: '',
                ordertype: 0,
                sumprice: '',
                yunprice: ''
            },

            goodslist: '',
            nextp: 0,
            count: 0
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            orderid: options.orderid
        });
        this.init();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        init() {
            var _this = this;

            app.globalData.http(
                'app/center/getorder',
                {
                    orderid: this.orderid
                },
                function (res) {
                    var arr = []; // for(var key in res.data.data.payinfo){
                    //   if(res.data.data.payinfo[key].selected == 'selected'){
                    //     arr.push(res.data.data.payinfo[key].title)
                    //   }
                    // }
                    // if(arr.length == 1){
                    //   _this.setData({
                    //     paytype: arr[0]
                    //   })
                    // }else if(arr.length == 2){
                    //   _this.setData({
                    //     paytype: '混合支付'
                    //   })
                    // }

                    _this.setData({
                        orderinfo: res.data,
                        goodslist: res.data.goodslist
                    });
                }
            );
        },

        delorder(e) {
            var that = this;
            var orderid = e.currentTarget.dataset.id;
            uni.showModal({
                title: '提示',
                content: '确定删除该订单吗？',

                success(v) {
                    if (v.confirm) {
                        app.globalData.http(
                            'Shop/delorder',
                            {
                                orderid: orderid
                            },
                            function (res) {
                                if (res.code == 0) {
                                    uni.showToast({
                                        title: res.msg
                                    });
                                    setTimeout(() => {
                                        uni.navigateBack({
                                            delta: 1
                                        });
                                    }, 1200);
                                }
                            }
                        );
                    }
                }
            });
        },

        cuidan() {
            uni.showLoading();
            setTimeout(() => {
                uni.hideLoading({
                    success: (res) => {}
                });
                uni.showToast({
                    title: '已为您催单'
                });
            }, 500);
        },

        cancelorder(e) {
            var _this = this;

            var orderid = e.currentTarget.dataset.id;
            uni.showModal({
                title: '温馨提示',
                content: '是否要取消该订单?',
                success: function (res) {
                    if (res.confirm) {
                        app.globalData
                            .http()
                            .unorder({
                                orderid: orderid
                            })
                            .then((res) => {
                                _this.setData({
                                    nextp: 1,
                                    count: 1
                                });

                                _this.init();
                            })
                            .catch((res) => {});
                    }
                }
            });
        },

        gopay(e) {
            var orderid = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: '/pages/order/submitorder?orderid=' + orderid
            });
        },

        buyagain() {
            for (var i = 0; i < this.goodslist.length; i++) {
                this.nums.push(this.goodslist[i].num);
                this.gids.push(this.goodslist[i].gid);
                this.guiges.push(JSON.parse(this.goodslist[i].guige));
            }

            this.setData({
                gids: this.gids,
                guiges: this.guiges,
                nums: this.nums
            });
            app.globalData.http(
                'Shop/createorder',
                {
                    gids: this.gids,
                    guiges: this.guiges,
                    nums: this.nums,
                    ordercode: new Date().getTime()
                },
                function (res) {
                    if (res.code == 0) {
                        uni.navigateTo({
                            url: '/pages/order/submitorder?orderid=' + res.orderid
                        });
                    }
                }
            );
        },

        copy() {
            uni.setClipboardData({
                data: this.orderinfo.orderid,

                success(res) {
                    app.globalData.showtoast('复制成功');
                }
            });
        },

        shouhuo(e) {
            var _this = this;

            var orderid = e.currentTarget.dataset.id;
            uni.showModal({
                title: '温馨提示',
                content: '是否要确认收货，请收到货后再进行确认',
                success: function (res) {
                    if (res.confirm) {
                        app.globalData.http(
                            'Shop/shouhuo',
                            {
                                orderid: orderid
                            },
                            function (res) {
                                _this.setData({
                                    nextp: 1,
                                    count: 1
                                });

                                _this.init();
                            }
                        );
                    }
                }
            });
        }
    }
};
</script>
<style lang="scss"> @import 'main.scss';
@import './msg.scss';
</style>
